﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using UnityEditor;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using UnityEditor.Callbacks;

/// 安卓打包工具
public class PackageTool : EditorWindow
{
	
	private static EditorWindow Instance;

	private const string localRootPath = "/TJSDK/PackTool";
	private static string resourcePath, InfoFilePath, dataFilePath;
	private static string[] Platforms, InfoOptions, SelectOptions;
	private Vector2 channelListScrollPosition, channelInfoScrollPosition, selectListScrollPosition;

	private static Dictionary<string,Dictionary<string, string>[]> channelConfigs = new Dictionary<string, Dictionary<string, string>[]> ();
	private static List<string> channelIdList = new List<string> ();
	private static List<string> channelCnList = new List<string> ();

	private static bool[] isSelected;

	private enum ConfigShowOption
	{
		info = 0,
		data = 1
	}

	private enum MultiOption
	{
		single = 0,
		multiple = 1
	}

	private static int configShowOption = 0;
	private static int multiOption = 0;
	private static bool isSelectAll;

	static int curChannelIndex = 0;

	[MenuItem ("Tools/打包工具")]
	public static void ShowWindow ()
	{
		resourcePath = UnityEngine.Application.dataPath + localRootPath;
		InfoFilePath = "Assets" + localRootPath + "/ChannelConfig.xml";

		SelectOptions = new string[2]{ "单选", "多选" };
		InfoOptions = new string[2]{ "渠道基础信息", "渠道数据信息" };
		ReadConfigXML ();
		Instance = EditorWindow.GetWindow (typeof(PackageTool), false, "打包工具");
	}

	#if UNITY_5
	void OnFocus ()
	{
		ShowWindow ();
	}
	#endif

	void OnProjectChange ()
	{
		ShowWindow ();
	}

	static void ReadConfigXML ()
	{
		try {
			XmlDocument xml = new XmlDocument ();
			xml.Load (InfoFilePath);
			XmlNode channelRoot = xml.SelectSingleNode ("ChannelConfig");
			channelConfigs.Clear ();
			foreach (XmlNode channel in channelRoot.ChildNodes) {
				Dictionary<string, string>[] channelConfig = new Dictionary<string, string>[3];
				channelConfig [0] = new Dictionary<string, string> ();
				channelConfig [1] = new Dictionary<string, string> ();
				channelConfig [2] = new Dictionary<string, string> ();

				string channelId = channel.SelectSingleNode ("ChannelId").InnerText;

				XmlNode channelInfo = channel.SelectSingleNode ("ChannelInfo");
				XmlNode channelData = channel.SelectSingleNode ("ChannelData");
				XmlNode channelMeta = channel.SelectSingleNode ("ChannelMeta");
				foreach (XmlNode element in channelInfo.ChildNodes) {
					channelConfig [0].Add (element.Name, element.InnerText);
				}

				channelConfig [1].Add ("ChannelId", channelId);
				foreach (XmlNode element in channelData.ChildNodes) {
					channelConfig [1].Add (element.Name, element.InnerText);
				}

				foreach (XmlNode element in channelMeta.ChildNodes) {
					channelConfig [2].Add (element.Name, element.InnerText);
				}

				channelConfigs.Add (channelId, channelConfig);
			}
			ShowChannel ();
		} catch (Exception e) {
			throw e;
		}
	}

	static void ShowChannel ()
	{
		channelIdList.Clear ();
		channelCnList.Clear ();
		isSelected = new bool[channelConfigs.Count];

		foreach (string key in channelConfigs.Keys) {
			channelIdList.Add (key);
			channelCnList.Add (channelConfigs [key] [0] ["ChannelCN"]);
		}
		curChannelIndex = 0;
	}

	bool isEditorChannelInfo = false;
	Dictionary<string, string> updateChannelIf = new Dictionary<string, string> ();
	Dictionary<string, string> tmpChannelIf = new Dictionary<string, string> ();
	string tmpIfValue = null;

	void ShowChannelInfo (int index)
	{
		Dictionary<string, string> channelInfo = channelConfigs [channelIdList [index]] [0];
		tmpChannelIf.Clear ();
		foreach (string key in channelInfo.Keys) {
			GUILayout.BeginHorizontal ();
			EditorGUILayout.LabelField (InfoType.GetInfoTypeName (key), GUILayout.Width (150));
			if (!isEditorChannelInfo) {
				EditorGUILayout.SelectableLabel (channelInfo [key], GUILayout.Height (18));
			} else {
				tmpIfValue = EditorGUILayout.TextField (updateChannelIf [key], GUILayout.Height (18));
				if (tmpIfValue != updateChannelIf [key]) {
					if (!tmpChannelIf.ContainsKey (key)) {
						tmpChannelIf.Add (key, tmpDtValue);
					}
					tmpChannelIf [key] = tmpDtValue;
				}
				if (updateChannelIf [key] == channelInfo [key]) {
					EditorGUILayout.LabelField ("", GUILayout.Width (40));
				} else {
					EditorGUILayout.LabelField ("已更新", GUILayout.Width (40));
				}
			}
			GUILayout.EndHorizontal ();
		}
		foreach (string key in tmpChannelIf.Keys) {
			updateChannelIf [key] = tmpChannelIf [key];
		}
	}

	bool isEditorChannelData = false;
	Dictionary<string, string> updateChannelDt = new Dictionary<string, string> ();
	Dictionary<string, string> tmpChannelDt = new Dictionary<string, string> ();
	string tmpDtValue = null;

	void ShowChannelData (int index)
	{
		tmpChannelDt.Clear ();
		Dictionary<string,string> channelDt = channelConfigs [channelIdList [index]] [1];
		foreach (string key in channelDt.Keys) {
			GUILayout.BeginHorizontal ();
			EditorGUILayout.LabelField (DataType.GetDataTypeName (key), GUILayout.Width (150));
			if (!isEditorChannelData) {
				EditorGUILayout.SelectableLabel (channelDt [key], GUILayout.Height (18));
			} else {
				tmpDtValue = EditorGUILayout.TextField (updateChannelDt [key], GUILayout.Height (18));
				if (tmpDtValue != updateChannelDt [key]) {
					if (!tmpChannelDt.ContainsKey (key)) {
						tmpChannelDt.Add (key, tmpDtValue);
					}
					tmpChannelDt [key] = tmpDtValue;
				}
				if (updateChannelDt [key] == channelDt [key]) {
					EditorGUILayout.LabelField ("", GUILayout.Width (40));
				} else {
					EditorGUILayout.LabelField ("已更新", GUILayout.Width (40));
				}
			}
			GUILayout.EndHorizontal ();
		}
		foreach (string key in tmpChannelDt.Keys) {
			updateChannelDt [key] = tmpChannelDt [key];
		}
	}

	void OnGUI ()
	{
		if (channelConfigs.Count == 0) {
			return;
		}

		GUILayout.Space (20);
		multiOption = GUILayout.Toolbar (multiOption, SelectOptions);
		GUILayout.Space (20);

		if (multiOption == (int)MultiOption.single) {
			int cIndex = GUILayout.Toolbar (curChannelIndex, channelCnList.ToArray ());
			if (cIndex != curChannelIndex) {
				curChannelIndex = cIndex;
				if (isEditorChannelData) {
					isEditorChannelData = false;
				}
				if (isEditorChannelInfo) {
					isEditorChannelInfo = false;
				}
			}

			GUILayout.Space (15);
			EditorGUILayout.LabelField (string.Format ("已选择渠道:  {0}", channelCnList [curChannelIndex]));
			GUILayout.Space (5);
			configShowOption = GUILayout.Toolbar (configShowOption, InfoOptions);
			GUILayout.Space (5);

			GUILayout.BeginVertical ("box");
			channelInfoScrollPosition = GUILayout.BeginScrollView (channelInfoScrollPosition, GUILayout.Height (210));

			if (configShowOption == (int)ConfigShowOption.data) {
				ShowChannelData (curChannelIndex);
			} else {
				ShowChannelInfo (curChannelIndex);
			}

			GUILayout.EndVertical ();
			GUILayout.EndScrollView ();

			if (configShowOption == (int)ConfigShowOption.data) {
				if (!isEditorChannelData) {
					if (GUILayout.Button ("编辑", GUILayout.Width (80), GUILayout.Height (25))) {
						updateChannelDt = channelConfigs [channelIdList [curChannelIndex]] [1];
						isEditorChannelData = true;
					}
				} else {
					GUILayout.BeginHorizontal ();
					if (GUILayout.Button ("更新", GUILayout.Width (80), GUILayout.Height (25))) {
						isEditorChannelData = false;
						ShowNotification (new GUIContent ("该功能暂不可用"));
					}
					if (GUILayout.Button ("取消", GUILayout.Width (80), GUILayout.Height (25))) {
						isEditorChannelData = false;
					}
					GUILayout.EndHorizontal ();
				}
			} else {
				if (!isEditorChannelInfo) {
					if (GUILayout.Button ("编辑", GUILayout.Width (80), GUILayout.Height (25))) {
						updateChannelIf = channelConfigs [channelIdList [curChannelIndex]] [0];
						isEditorChannelInfo = true;
					}
				} else {
					GUILayout.BeginHorizontal ();
					if (GUILayout.Button ("更新", GUILayout.Width (80), GUILayout.Height (25))) {
						isEditorChannelInfo = false;
						ShowNotification (new GUIContent ("该功能暂不可用"));
					}
					if (GUILayout.Button ("取消", GUILayout.Width (80), GUILayout.Height (25))) {
						isEditorChannelInfo = false;
					}
					GUILayout.EndHorizontal ();
				}
			}

			GUILayout.Space (15);

			GUILayout.BeginHorizontal ();
			if (GUILayout.Button ("仅设置", GUILayout.Width (150), GUILayout.Height (30))) {
				DoBuildTarget (channelConfigs [channelIdList [curChannelIndex]]);
			}
			if (GUILayout.Button ("设置并打包", GUILayout.Width (150), GUILayout.Height (30))) {
				DoBuildTarget (channelConfigs [channelIdList [curChannelIndex]], true);
			}
			GUILayout.EndHorizontal ();
		} else {
			isSelectAll = IsAllSelect ();
			bool tmp = GUILayout.Toggle (isSelectAll, "全选");
			if (isSelectAll != tmp) {
				isSelectAll = tmp;
				for (int i = 0; i < isSelected.Length; i++) {
					isSelected [i] = isSelectAll;
				}
			}

			GUILayout.Space (10);

			GUILayout.BeginHorizontal ();
			for (int i = 0; i < isSelected.Length; i++) {
				isSelected [i] = GUILayout.Toggle (isSelected [i], channelCnList [i]);
			}
			GUILayout.EndHorizontal ();

			GUILayout.Space (10);
			GUILayout.Label ("已选渠道:");
			GUILayout.Space (10);

			GUILayout.BeginVertical ("box", GUILayout.Height (200));
			selectListScrollPosition = GUILayout.BeginScrollView (selectListScrollPosition, GUILayout.Height (200));
			GUILayout.Label ("渠道列表:");
			int index = 0;
			string selectList = string.Empty;
			for (int i = 0; i < isSelected.Length; i++) {
				if (isSelected [i]) {
					index++;
					selectList += string.Format ("{0}:{1}", index, channelCnList [i]) + "\n";
				}
			}
			GUILayout.Label (selectList);
			GUILayout.EndScrollView ();
			GUILayout.EndVertical ();

			GUILayout.Space (15);

			GUILayout.BeginHorizontal ();
			if (GUILayout.Button ("全部设置", GUILayout.Width (150), GUILayout.Height (30))) {
				if (IsNoneSelect ()) {
					ShowNotification (new GUIContent ("没有选择任何渠道"));
				}
				DoBuildMultTarget (isSelected, false);
			}
			if (GUILayout.Button ("全部设置并打包", GUILayout.Width (150), GUILayout.Height (30))) {
				if (IsNoneSelect ()) {
					ShowNotification (new GUIContent ("没有选择任何渠道"));
				}
				DoBuildMultTarget (isSelected, true);
			}
			GUILayout.EndHorizontal ();
		}
	}

	static void DoBuildMultTarget (bool[] selectOps, bool isBuild)
	{
		bool[] tmpSelectOps = new bool[selectOps.Length];
		for (int i = 0; i < selectOps.Length; i++) {
			tmpSelectOps [i] = selectOps [i];
		}
		for (int i = 0; i < tmpSelectOps.Length; i++) {
			if (tmpSelectOps [i]) {
				DoBuildTarget (channelConfigs [channelIdList [i]], isBuild);
			}
		}
	}



	static bool IsAllSelect ()
	{
		for (int i = 0; i < isSelected.Length; i++) {
			if (!isSelected [i]) {
				return false;
			}
		}
		return true;
	}

	static bool IsNoneSelect ()
	{
		for (int i = 0; i < isSelected.Length; i++) {
			if (isSelected [i]) {
				return false;
			}
		}
		return true;
	}



	static void DoBuildTarget (Dictionary<string, string>[] infoDic, bool isBuild = false)
	{
		string target_dir = string.Empty;
		string target_name = string.Empty;
		BuildTargetGroup targetGroup = BuildTargetGroup.Unknown;
		BuildTarget buildTarget = BuildTarget.Android;

		target_name = PlayerSettings.productName + "_" + infoDic [0] ["ChannelCN"] + "_" + PlayerSettings.bundleVersion + "_" + System.DateTime.Now.ToString ("MMddHHmm") + ".apk";
		target_dir = UnityEngine.Application.dataPath.Replace ("/Assets", "") + "/Android/";
		targetGroup = BuildTargetGroup.Android;
		buildTarget = BuildTarget.Android;
		EditorUserBuildSettings.SwitchActiveBuildTarget (BuildTarget.Android);
		PlayerSettings.Android.targetDevice = AndroidTargetDevice.ARMv7;
		SetAndroidSign ();

		if (Directory.Exists (target_dir)) {
			if (File.Exists (target_name)) {
				File.Delete (target_name);
			}
		} else {
			Directory.CreateDirectory (target_dir);
		}

		foreach (var item in infoDic[0]) {
			switch (item.Key) {
			default:
				break;
			case "BundleId":
				#if UNITY_5_6_OR_NEWER
				PlayerSettings.applicationIdentifier = item.Value;
				#else
				PlayerSettings.bundleIdentifier = item.Value;
				#endif
				break;
			case "DefineSymbol":
				PlayerSettings.SetScriptingDefineSymbolsForGroup (targetGroup, item.Value);
				break;
			case "ProductName":
				PlayerSettings.productName = item.Value;
				break;
			case "BundleVersion":
				PlayerSettings.bundleVersion = item.Value;
				break;
			case "BundleVersionCode":
				PlayerSettings.Android.bundleVersionCode = int.Parse (item.Value);
				break;
			case "Icon":
				ChangeIcon (targetGroup, item.Value);
				break;
			case "Splash":
				ChangeSplash (targetGroup, item.Value);
				break;
			case "AndroidLib":
				ChangeAndroidLib (item.Value);
				break;
			}
		}

		ChannelChecker.ChangeChannel (infoDic [1], true);

		string path = UnityEngine.Application.dataPath + "/Plugins/Android/AndroidManifest.xml";
		foreach (var item in infoDic[2]) {
			if (Regex.IsMatch (item.Value, @"(?i)^[0-9]+$")) {
				float num = float.Parse (item.Value);
				if (num > 2147483647) {
					Function.UpdateXMLMetaData (path, item.Key, "\\ " + item.Value);
				} else {
					Function.UpdateXMLMetaData (path, item.Key, item.Value);
				}
			} else {
				Function.UpdateXMLMetaData (path, item.Key, item.Value);
			}
		}

		if (isBuild) {
			GenericBuild (FindEnabledEditorScenes (), target_dir + target_name, buildTarget, BuildOptions.None);
		} else {
			Instance.ShowNotification (new GUIContent ("已完成设置"));
		}
	}

	[PostProcessBuild (100)]
	public static void OnPostProcessBuild (BuildTarget target, string pathToBuiltProject)
	{
		Instance.ShowNotification (new GUIContent ("已完成打包"));
		string[] paths = pathToBuiltProject.Replace ('\\', '/').Split ('/');
		pathToBuiltProject = null;
		for (int i = 0; i < paths.Length - 1; i++) {
			pathToBuiltProject += paths [i] + "/";
		}
		Debug.Log ("pathToBuiltProject = " + pathToBuiltProject);
		UnityEngine.Application.OpenURL (pathToBuiltProject);
	}

	static void SetAndroidSign ()
	{
		PlayerSettings.Android.keystoreName = UnityEngine.Application.dataPath + localRootPath + "/tomatojoy.keystore";
		PlayerSettings.Android.keystorePass = "tomatojoy";
		PlayerSettings.Android.keyaliasName = "tomatojoy";
		PlayerSettings.Android.keyaliasPass = "tomato";
	}

	static void ChangeIcon (BuildTargetGroup group, string iconName)
	{
		string sourcetexPath = string.Format ("{0}/IconLib/{1}.png", resourcePath, iconName);
		SetTextureFormat (GetRelativeAssetPath (sourcetexPath));

		sourcetexPath = GetRelativeAssetPath (sourcetexPath);
		Texture2D sourcetex = AssetDatabase.LoadAssetAtPath (sourcetexPath, typeof(Texture2D)) as Texture2D;
		
		Texture2D[] icons = new Texture2D[] {
			sourcetex,
			sourcetex,
			sourcetex,
			sourcetex,
			sourcetex,
			sourcetex,
		};
		
		PlayerSettings.SetIconsForTargetGroup (group, icons);
		AssetDatabase.Refresh ();
		Debug.Log ("完成Icon替换!");
	}

	static void ChangeSplash (BuildTargetGroup group, string splashName)
	{
		string sourcetexPath = string.Format ("{0}/SplashLib/{1}.png", resourcePath, splashName);
		string targetPath = string.Format ("{0}/splash.png", resourcePath);
		
		if (!File.Exists (sourcetexPath)) {
			Debug.LogError ("找不到" + sourcetexPath + "文件");
			Debug.LogError ("Splash替换中止！");
			return;
		}
		if (!File.Exists (targetPath)) {
			byte[] bytes0 = new byte[1];
			File.WriteAllBytes (targetPath, bytes0);
		}
		
		SetTextureFormat (GetRelativeAssetPath (sourcetexPath));
		SetTextureFormat (GetRelativeAssetPath (targetPath));
		
		sourcetexPath = GetRelativeAssetPath (sourcetexPath);
		Texture2D sourcetex = AssetDatabase.LoadAssetAtPath (sourcetexPath, typeof(Texture2D)) as Texture2D;
		byte[] bytes = sourcetex.EncodeToPNG ();
		File.WriteAllBytes (targetPath, bytes);
		SetTextureFormat (GetRelativeAssetPath (targetPath));


		#if UNITY_5_4
		PlayerSettings.showUnitySplashScreen = false;
		#elif UNITY_5_6_OR_NEWER
		PlayerSettings.SplashScreen.show = false;
		PlayerSettings.SplashScreen.showUnityLogo = false;
		#endif

		PlayerSettings.Android.splashScreenScale = AndroidSplashScreenScale.ScaleToFill;
		AssetDatabase.Refresh ();
		Debug.Log ("完成Splash替换!");
	}

	static void SetTextureFormat (string _relativeAssetPath)
	{
		TextureImporter ti = (TextureImporter)TextureImporter.GetAtPath (_relativeAssetPath);
		if (ti == null) {
			Debug.LogError (string.Format ("未找到 {0} 文件", _relativeAssetPath));
			return;
		}
		ti.textureType = TextureImporterType.Sprite;
		ti.isReadable = true;
		ti.mipmapEnabled = false;
		ti.textureFormat = TextureImporterFormat.AutomaticTruecolor;
		ti.maxTextureSize = 2048;
		#if UNITY_5_6_OR_NEWER
		ti.textureCompression = TextureImporterCompression.Uncompressed;
		#endif
		AssetDatabase.ImportAsset (_relativeAssetPath);
	}

	static string GetRelativeAssetPath (string _fullPath)
	{
		_fullPath = _fullPath.Replace ("\\", "/");
		int idx = _fullPath.IndexOf ("Assets");
		string assetRelativePath = _fullPath.Substring (idx);
		return assetRelativePath;
	}

	static void ChangeAndroidLib (string sourceName)
	{
		if (Directory.Exists (UnityEngine.Application.dataPath + "/Plugins/Android")) {
			if (!Directory.Exists (UnityEngine.Application.dataPath.Replace ("/Assets", "/PluginsOld"))) {
				Directory.CreateDirectory (UnityEngine.Application.dataPath.Replace ("/Assets", "/PluginsOld"));
			}
			Function.RenameDirectory (UnityEngine.Application.dataPath + "/Plugins/Android"
				, UnityEngine.Application.dataPath.Replace ("/Assets", "/PluginsOld/Android_" + System.DateTime.Now.Ticks));
		}
		AssetDatabase.Refresh ();
		string target_dir = UnityEngine.Application.dataPath.Replace ("/Assets", "/PluginsLibs/" + sourceName);
		if (Directory.Exists (target_dir)) {
			Function.CopyDirectory (target_dir, UnityEngine.Application.dataPath + "/Plugins/Android");
			AssetDatabase.Refresh ();
			Debug.Log ("Android plugins:" + sourceName + "替换完成！");
		} else {
			Debug.LogError ("不存在" + target_dir + "文件夹");
			Debug.LogError ("Android plugins:" + sourceName + "替换中止！");
		}
	}

	private static string[] FindEnabledEditorScenes ()
	{
		List<string> EditorScenes = new List<string> ();
		foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) {
			if (!scene.enabled)
				continue;
			EditorScenes.Add (scene.path);
		}
		return EditorScenes.ToArray ();
	}

	static void GenericBuild (string[] scenes, string target_dir, BuildTarget build_target, BuildOptions build_options)
	{
		Debug.Log (build_target);
		Debug.Log (target_dir);

		EditorUserBuildSettings.SwitchActiveBuildTarget (build_target);
		string res = BuildPipeline.BuildPlayer (scenes, target_dir, build_target, build_options);

		if (res.Length > 0) {
			throw new Exception ("BuildPlayer failure: " + res);
		}
	}

	//写入(ClassName).cs数据
	public class ChannelChecker
	{
		const string ClassName = "SDKData";
		static string TargetCodeFile = UnityEngine.Application.dataPath + localRootPath + "/" + ClassName + ".cs";

		public static string ChangeChannel (Dictionary<string,string> channelDataDic, bool isOverWrite = false)
		{
			if (!File.Exists (TargetCodeFile)) {
				FileStream file = File.Create (TargetCodeFile);
				file.Close ();
				AssetDatabase.Refresh ();
			}

			using (StreamWriter writer = new StreamWriter (TargetCodeFile, false)) {
				try {
					string code = GenerateCode (channelDataDic, isOverWrite, TargetCodeFile);
					writer.WriteLine ("{0}", code);
				} catch (System.Exception ex) {
					string msg = " threw:\n" + ex.ToString ();
					Debug.LogError (msg);
				}
				writer.Close ();
				AssetDatabase.Refresh ();
			}
			return TargetCodeFile;
		}

		static string GenerateCode (Dictionary<string,string> channelDataDic, bool isOverWrite, string filePath)
		{
			string code = string.Empty;
			code += "/// this is " + channelDataDic ["ChannelId"] + " channel info\n";
			code += "public static class " + ClassName + "\n{\n";

			if (!isOverWrite) {
				Debug.Log ("不重写数据");
				string codes = null;
				List<string> codeArr = null;
				try {
					codes = File.ReadAllText (filePath, System.Text.Encoding.UTF8);
					codeArr = new List<string> (File.ReadAllLines (filePath, System.Text.Encoding.UTF8));
					Debug.Log ("codeArr:" + codeArr.Count);
				} catch (System.Exception ex) {
					string msg = " threw:\n" + ex.ToString ();
					Debug.LogError (msg);
				}

				if (codeArr.Count < 3 || !codes.Contains ("public static class " + ClassName)) {
					isOverWrite = true;
					Debug.LogError ("这里有问题:" + codeArr.Count);
				} else {
					foreach (string key in channelDataDic.Keys) {
						if (codes.Contains (channelDataDic [key])) {
							for (int i = 0; i < codeArr.Count; i++) {
								if (codeArr [i].Contains (channelDataDic [key])) {
									codeArr [i] = string.Format ("\tpublic const string {0} = \"{1}\";", key, channelDataDic [key]);
									break;
								}
							}
						} else {
							for (int i = codeArr.Count - 1; i >= 0; i--) {
								if (codeArr [i] == "}") {
									codeArr.Insert (i, string.Format ("\tpublic const string {0} = \"{1}\";", key, channelDataDic [key]));
									break;
								}
							}
						}
					}
					for (int i = 2; i < codeArr.Count; i++) {
						code += codeArr [i] + "\n";
					}
				}
			}
			if (isOverWrite) {
//				Debug.Log ("重写数据");
				foreach (string key in channelDataDic.Keys) {
					code += string.Format ("\tpublic const string {0} = \"{1}\";\n", key, channelDataDic [key]);
				}
				code += "}";
			} 
			return code;
		}
	}
}